﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Soul.XLS
{
    public class ExcelMergeRegionCollection : ICollection<ExcelMergeRegion>
    {
        private readonly List<ExcelMergeRegion> _items = new List<ExcelMergeRegion>();

        public int Count => _items.Count;

        public bool IsReadOnly => false;

        public void Add(int firstRow, int lastRow, int firstColumn, int lastColumn)
        {
            if (_items.Any(a => a.FirstRow <= firstRow && a.FirstColumn <= firstColumn && a.LastRow >= lastRow && a.LastColumn >= lastColumn))
            {
                return;
            }
            _items.Add(new ExcelMergeRegion(firstRow, lastRow, firstColumn, lastColumn));
        }

        public void Add(ExcelMergeRegion item)
        {
            Add(item.FirstRow, item.LastRow, item.FirstColumn, item.LastColumn);
        }

        public void Clear()
        {
            _items.Clear();
        }

        public bool Contains(ExcelMergeRegion item)
        {
            return _items.Contains(item);
        }

        public void CopyTo(ExcelMergeRegion[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public IEnumerator<ExcelMergeRegion> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        public bool Remove(ExcelMergeRegion item)
        {
            return _items.Remove(item);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_items).GetEnumerator();
        }
    }
}
